/* eslint-disable no-shadow */
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
const state = {
  visitedViews: [],
};

// mutations
const mutations = {
  ADD_VISITED_VIEWS: (oldState, view) => {
    console.log(oldState);
    if (oldState.visitedViews.some((v) => v.path === view.path)) return;
    oldState.visitedViews.push({
      name: view.name,
      path: view.path,
    });
  },
  DEL_VISITED_VIEWS: (oldState, view) => {
    for (const [i, v] of oldState.visitedViews.entries()) {
      if (v.path === view.path) {
        oldState.visitedViews.splice(i, 1);
        break;
      }
    }
  },
  DEL_OTHERS_VIEWS: (oldState, view) => {
    for (const [i, v] of oldState.visitedViews.entries()) {
      if (v.path === view.path) {
        oldState.visitedViews = oldState.visitedViews.slice(i, i + 1);
        break;
      }
    }
  },
  DEL_ALL_VIEWS: (oldState) => {
    oldState.visitedViews = [];
  },
};

const actions = {
  addVisitedViews({ commit }, view) {
    console.log(view, "333");
    commit("ADD_VISITED_VIEWS", view);
  },
  delVisitedViews({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_VISITED_VIEWS", view);
      resolve([...state.visitedViews]);
    });
  },
  delOthersViews({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_OTHERS_VIEWS", view);
      resolve([...state.visitedViews]);
    });
  },
  delAllViews({ commit, state }) {
    return new Promise((resolve) => {
      commit("DEL_ALL_VIEWS");
      resolve([...state.visitedViews]);
    });
  },
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
